package com.qiang.demo05.dao;

import com.qiang.demo05.domain.Student;

import java.util.ArrayList;

public class StudentDaoImp implements IstudentDao {

    private static ArrayList<Student> array = new ArrayList<>();

    static {
        Student s1 = new Student("001", "张三", 23, "河北");
        Student s2 = new Student("002", "李四", 24, "山东");
        Student s3 = new Student("003", "王五", 21, "河南");
        array.add(s1);
        array.add(s2);
        array.add(s3);
    }

    @Override
    public void addStudent(Student st) {
        array.add(st);
    }

    @Override
    public void deleteStudent(String id) {
        array.remove(id);
    }

    @Override
    public void updateStudent(String id, Student newst) {

        int index = getIndex(id);
        array.set(index, newst);
    }


    //查看需要返回学生信息
    @Override
    public ArrayList<Student> findStudent() {
        return array;
    }

    //判断id是否存在

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            if (array.size() != 0 && array.get(i).getId().equals(id)) {
                index = i;
                return index;
            }
        }
        return index;

    }
}
